package org.korosoft.rusalad.syncnrun.cli;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.HelpFormatter;
import org.korosoft.rusalad.syncnrun.Main;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Usage command.
 *
 * @author Dmitry Korotkov
 * @since 1.0
 */
public class CliHelp extends CliCommand {

    public CliHelp() {
        super("help", "Shows this help message");
    }

    @Override
    public void doExecute(CommandLine commandLine) {
        List<CliCommand> commands = new ArrayList<CliCommand>(Main.commands.values());
        Collections.sort(commands, new Comparator<CliCommand>() {
            @Override
            public int compare(CliCommand o1, CliCommand o2) {
                return o1.getName().compareTo(o2.getName());
            }
        });

        System.out.println("USAGE: syncrun <command> [options]");
        System.out.println();
        System.out.println("Supported commands: ");

        PrintWriter pw = new PrintWriter(System.out);

        for (CliCommand command : commands) {
            System.out.println();
            System.out.println(command.getName() + " - " + command.getDescription());
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp(pw, 75, "syncrun " + command.getName(), "", command.getOptions(), 4, 4, "", true);
            pw.flush();
        }
    }
}
